import { createStore } from 'vuex'
import { login, getCode } from '@/api/login'
import { setToken } from '@/utils/auth'

export default createStore({
  state: {
    token: null
  },
  mutations: {
    setToken (state, token) {
      state.token = token
    }
  },
  actions: {
    login ({ commit }, loginForm = { username: 'hykf', password: 'hykf12#$' }) {
      return new Promise((resolve, reject) => {
        login(loginForm).then((res: any) => {
          const { data } = res
          const token = data.access_token
          commit('setToken', token)
          setToken(token)
          resolve(token)
        }).catch(err => {
          reject(err)
        })
      })
    },
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    getCode ({ commit }, token) {
      return new Promise((resolve, reject) => {
        getCode(token).then((res: any) => {
          console.log('res: ', res)
        }).catch(err => {
          reject(err)
        })
      })
    }
  },
  modules: {
  }
})
